<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Question;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class QuestionController extends Controller
{
    /**
     * 问题反馈
     * @return array
     */
    public function addQuestion(){
        if($input=Input::except('_token')){
            if(trim($input['question'])==''){
                return ['status'=>0,'msg'=>'内容不能为空'];
            }
            if(!empty(Session::get('user')['id'])){
                $input['from_user']=Session::get('user')['id'];
            }
            return Question::create($input)?['status'=>1,'msg'=>'反馈成功，谢谢']:['status'=>0,'msg'=>'反馈失败'];
        }
    }

    /**收件箱
     * @return $this
     */
    public function inBox(){
        if($id=Input::except('_token')){
            $one=DB::table('question')->select('user.username','user.avatar','question.created_at','question.question','question.id')
                ->join('user','user.id','=','question.from_user','left')->where('question.id',$id)->get();
            DB::table('question')->where('id',$id)->update(['is_read'=>1]);
            return $one;
        }
        $count=Question::count();
        $counti=Question::where('is_read',0)->count();
        $questions=Question::select('user.username','user.avatar','question.created_at','question.is_read','question.question','question.id')
            ->join('user','user.id','=','question.from_user','left')->orderBy('question.id','desc')
            ->get()->all();
        return view('admin.inbox.inbox',compact('counti','count','questions'))->with(['title'=>'收件箱']);
    }

    /**
     * 删除问题反馈
     * @return array
     */
    public function deleteQuestion(){
        if($id=Input::except('_token')){
            return Question::where('id',$id)->delete()?['status'=>1,'msg'=>'删除完成']:['status'=>0,'msg'=>'删除失败'];
        }
    }

}
